package com.yiyi.credit.manage.abutment.provider;

import com.yiyi.credit.manage.abutment.common.enums.ProductStatus;
import com.yiyi.credit.manage.abutment.common.enums.ProductType;
import com.yiyi.credit.manage.abutment.common.utils.CommonUtil;
import com.yiyi.credit.manage.model.po.ProductPo;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Map;


public class CcxiProvider {

    public String selectInterfaceFlowByReqData(Map<String, Object> para) {

        return new SQL() {{
            SELECT("id,projectCode,platformCode,interfaceType,account,requestNo,reqData,respData,resCode,resMsg,projectReqDate,projectRespDate,createTIme,updateTIme");
            FROM("interface_flow");
            StringBuilder whereSql = new StringBuilder(" 1=1 ");
            buildWhere(para, whereSql);
            WHERE(whereSql.toString());
            ORDER_BY("id desc");
        }}.toString();
    }

    private void buildWhere(Map<String, Object> para, StringBuilder where) {

        String interfaceType = (String) para.get("interfaceType");
        String cid = (String) para.get("cid");
        String name = (String) para.get("name");
        String mobile = (String) para.get("mobile");
        String card = (String) para.get("card");
        String type = (String) para.get("type");
        String key = (String) para.get("key");
        String cat = (String) para.get("cat");
        String month = (String) para.get("month");

        if (!StringUtils.isEmpty(interfaceType)) {
            where.append(String.format(" and interfaceType = '%s' ", interfaceType));
        }
        if (!StringUtils.isEmpty(cid)) {
            where.append(String.format(" and JSON_EXTRACT(reqData,'$.cid') = '%s' ", cid));
        }
        if (!StringUtils.isEmpty(name)) {
            where.append(String.format(" and JSON_EXTRACT(reqData,'$.name') = '%s' ", name));
        }
        if (!StringUtils.isEmpty(mobile)) {
            where.append(String.format(" and JSON_EXTRACT(reqData,'$.mobile') = '%s' ", mobile));
        }
        if (!StringUtils.isEmpty(card)) {
            where.append(String.format(" and JSON_EXTRACT(reqData,'$.card') = '%s' ", card));
        }
        if (!StringUtils.isEmpty(type)) {
            where.append(String.format(" and JSON_EXTRACT(reqData,'$.type') = '%s' ", type));
        }
        if (!StringUtils.isEmpty(key)) {
            where.append(String.format(" and JSON_EXTRACT(reqData,'$.key') = '%s' ", key));
        }
        if (!StringUtils.isEmpty(cat)) {
            where.append(String.format(" and JSON_EXTRACT(reqData,'$.cat') = '%s' ", cat));
        }
        if (!StringUtils.isEmpty(month)) {
            where.append(String.format(" and JSON_EXTRACT(reqData,'$.month') = '%s' ", month));
        }
        where.append(" and respData is not NULL ");
    }
}
